package cn.jiyun.controller;

import cn.jiyun.common.R;
import cn.jiyun.pojo.Employee;
import cn.jiyun.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import javax.websocket.Session;

@RequestMapping("employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
    R<Employee> r=employeeService.login(employee);
    if (r.getCode()==1){
        System.out.println("---------------------"+r.getData().getId());
        session.setAttribute("employee",r.getData().getId());
    }

        return employeeService.login(employee);
    }
//http://localhost:9090/employee/page?page=1&pageSize=10
    @PostMapping("logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }

    @GetMapping("page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        QueryWrapper<Employee> qw=new QueryWrapper<>();
        Page<Employee> page1=new Page<>(page,pageSize);
        qw.like(name!=null,"name",name);
        return employeeService.page(page1,qw);
    }

    @PostMapping("add")
    public R add(@RequestBody Employee employee,HttpSession session){
        long id= (long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setCreateUser(id);
        return employeeService.add(employee);
    }

    @PutMapping()
    public R zt(@RequestBody Employee employee,HttpSession session){
        long id= (long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        return employeeService.zt(employee);
    }

    @GetMapping("hx")
    public R hx( int id){
        Employee employee=employeeService.hx(id);
        return R.success(employee);
    }

//    @GetMapping("{id}")
//    public R hx(@PathVariable Long id){
//        Employee employee=employeeService.hx(id);
//        return R.success(employee);
//    }



    @PostMapping("update")
    public R update(@RequestBody Employee employee){
        return employeeService.update(employee);
    }
}
